// TestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "..\DCMlicense.h"
#include "Test.h"
#include "TestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// DCMLicenseGenDlg dialog

TCHAR* g_LicNames[] = 
{
	 TEXT("Generic License")
	,TEXT("Wintube")
};

CDCMLicense::eLicenseAppType g_LicIds[] = 
{ 
	 CDCMLicense::kLicAppTypeNone
	,CDCMLicense::kLicAppTypeWintube
};




DCMLicenseGenDlg::DCMLicenseGenDlg(CWnd* pParent /*=NULL*/)
	: CDialog(DCMLicenseGenDlg::IDD, pParent)
	, m_KeyVal(_T(""))
	, m_strOptionsMask(_T(""))
	, m_bProEdition(FALSE)
	, m_bOptionLite(FALSE)
	, m_bOptionNoTimeout(FALSE)
	, m_bOptionNoExpire(FALSE)
	, m_bIsRegistered(FALSE)
	, m_bIPOD(FALSE)
	, m_ProdId(0)
	, m_Version(0)
	, m_TimeStamp(0)
	, m_Date(0)
	, m_Time(0)
	, m_bEmbededUID(FALSE)
	, m_Uid(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void DCMLicenseGenDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_LIC_TYPE, m_CtrlLicType);
	DDX_Control(pDX, IDC_BUTTON_DECODE_CUSTOM_KEY, m_BtnDecodeCustom);
	DDX_Text(pDX, IDC_EDIT_KEY, m_KeyVal);
	DDX_Text(pDX, IDC_OPTIONISMASK_EDIT, m_strOptionsMask);
	DDX_Check(pDX, IDC_CHECK_PRO_EDITION, m_bProEdition);
	DDX_Check(pDX, IDC_OPT_LITE_CHECK, m_bOptionLite);
	DDX_Check(pDX, IDC_NOTIMEOUT_OPT_CHECK, m_bOptionNoTimeout);
	DDX_Check(pDX, IDC_NOEXPIRE_OPT_CHECK, m_bOptionNoExpire);
	DDX_Check(pDX, IDC_CHECK_REGISTERED, m_bIsRegistered);
	DDX_Check(pDX, IDC_CHECK_IPOD, m_bIPOD);
	DDX_Text(pDX, IDC_EDIT_PRODUCT, m_ProdId);
	DDV_MinMaxUInt(pDX, m_ProdId, 0, 99);
	DDX_Text(pDX, IDC_EDIT_VERSION, m_Version);
	DDX_Text(pDX, IDC_EDIT_TIMESTAMP, m_TimeStamp);
	DDX_DateTimeCtrl(pDX, IDC_DATEPICKER, m_Date);
	DDX_DateTimeCtrl(pDX, IDC_TIMEPICKER, m_Time);
	DDX_Check(pDX, IDC_CHECK_EMBEDDED_UID, m_bEmbededUID);
	DDX_Text(pDX, IDC_EDIT_UID, m_Uid);
}

BEGIN_MESSAGE_MAP(DCMLicenseGenDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_CBN_SELCHANGE(IDC_COMBO_LIC_TYPE, &DCMLicenseGenDlg::OnCbnSelchangeComboLicType)
	ON_CBN_EDITCHANGE(IDC_COMBO_LIC_TYPE, &DCMLicenseGenDlg::OnCbnEditchangeComboLicType)
	ON_EN_CHANGE(IDC_EDIT_KEY, &DCMLicenseGenDlg::OnEnChangeEditKey)
	ON_EN_CHANGE(IDC_EDIT_VERSION, &DCMLicenseGenDlg::OnEnChangeEditVersion)
	ON_BN_CLICKED(IDC_BUTTON_RELOAD, &DCMLicenseGenDlg::OnBnClickedButtonReload)
	ON_BN_CLICKED(IDC_SETOPTIONS_BUTTON, &DCMLicenseGenDlg::OnBnClickedSetoptionsButton)
	ON_BN_CLICKED(IDC_BUTTON_COPY_KEY_2_CLIPBOARD, &DCMLicenseGenDlg::OnBnClickedButtonCopyKey2Clipboard)
	ON_BN_CLICKED(IDC_CHECK_PRO_EDITION, &DCMLicenseGenDlg::OnBnClickedCheckProEdition)
	ON_BN_CLICKED(IDC_OPT_LITE_CHECK, &DCMLicenseGenDlg::OnBnClickedOptLiteCheck)
	ON_BN_CLICKED(IDC_NOTIMEOUT_OPT_CHECK, &DCMLicenseGenDlg::OnBnClickedNotimeoutOptCheck)
	ON_BN_CLICKED(IDC_NOEXPIRE_OPT_CHECK, &DCMLicenseGenDlg::OnBnClickedNoexpireOptCheck)
	ON_BN_CLICKED(IDC_CHECK_REGISTERED, &DCMLicenseGenDlg::OnBnClickedCheckRegistered)
	ON_BN_CLICKED(IDC_CHECK_IPOD, &DCMLicenseGenDlg::OnBnClickedCheckIPOD)
	ON_BN_CLICKED(IDC_BUTTON_DECODE_CUSTOM_KEY, &DCMLicenseGenDlg::OnBnClickedButtonDecodeCustomKey)
	ON_BN_CLICKED(IDC_CHECK_EMBEDDED_UID, &DCMLicenseGenDlg::OnBnClickedCheckEmbeddedUid)
END_MESSAGE_MAP()


// DCMLicenseGenDlg message handlers

BOOL DCMLicenseGenDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	for(int i = 0 ; i < sizeof(g_LicNames)/sizeof(g_LicNames[0]); i++)
	{
		int idx = m_CtrlLicType.AddString(g_LicNames[i]);
		if(idx >= 0)
			m_CtrlLicType.SetItemData(idx, g_LicIds[i]);
	}
	m_CtrlLicType.SelectString(0, g_LicNames[0]);
	OnCbnSelchangeComboLicType();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void DCMLicenseGenDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR DCMLicenseGenDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void DCMLicenseGenDlg::OnCbnSelchangeComboLicType()
{
	// TODO: Add your control notification handler code here
	m_CurAppType = (CDCMLicense::eLicenseAppType) m_CtrlLicType.GetItemData(m_CtrlLicType.GetCurSel());
	m_CtrlLicType.GetLBText(m_CtrlLicType.GetCurSel(), m_CurAppName);
	m_BtnDecodeCustom.EnableWindow(FALSE);
	m_licSubType = CDCMLicense::kLicTypeApp;
	GetDlgItem(IDC_EDIT_KEY)->EnableWindow(FALSE);

	UpdateOptions();
}

void DCMLicenseGenDlg::OnCbnEditchangeComboLicType()
{
	// TODO: Add your control notification handler code here
	m_CurAppType = (CDCMLicense::eLicenseAppType) m_CtrlLicType.GetItemData(m_CtrlLicType.GetCurSel());
	m_CtrlLicType.GetLBText(m_CtrlLicType.GetCurSel(), m_CurAppName);

	UpdateOptions();

}


void DCMLicenseGenDlg::UpdateOptions(bool bUseCustomKey)
{
	UpdateData();

	UpdateOptionsNew(bUseCustomKey);
	UpdateOptionsFlags();
}


void DCMLicenseGenDlg::UpdateOptionsNew(bool bUseCustomKey)
{
	UpdateData();

	CDCMLicense::eLicenseAppType licAppType		= m_CurAppType;
	CDCMLicense::eLicenseAppType licAppType2	= CDCMLicense::kLicAppTypeNone;
	CDCMLicense::eLicenseType licSubType = m_licSubType;


	{

		CDCMLicense2 lic( licAppType, licSubType);
		CDCMLicense::SetApplication(m_CurAppType);
		unsigned __int64 opts = 0;;
		DWORD custId = 0;
		DWORD uid;
		if(bUseCustomKey)
		{
			CUTILprod prod(HKEY_CLASSES_ROOT, LIC_REGISTRY);
			CUTILprodinfo2 prodinfo;
		
			int res = 0;
			char *sz_KeyVal = NULL;
#ifdef UNICODE
			int len = m_KeyVal.GetLength();
			sz_KeyVal = new char[len+1];
			WideCharToMultiByte(CP_ACP,0,m_KeyVal.GetBuffer(),len,sz_KeyVal,len,NULL,NULL);
			sz_KeyVal[len]='\0';
#else
			sz_KeyVal = m_KeyVal.GetBuffer(0);
#endif
			if(m_bEmbededUID)
			{
				if((res = prod.TextToInfo(&prodinfo, &uid, sz_KeyVal)) >= 0)
					m_Uid.Format(TEXT("%08x"), uid);
			}
			else
			{
				uid = _tcstoul(m_Uid, NULL, 16);
				prod.Descramble(uid, sz_KeyVal, prodinfo.key, sizeof(prodinfo.key));
				res = prod.GenerateInfo(&prodinfo, uid);
			}
			if(res >= 0)
			{
				custId = prodinfo.customer;
				opts = prodinfo.option;
				m_ProdId = prodinfo.product;
				m_Version = prodinfo.version;
			}
		}
		else
		{
			custId = lic.GetCustId();

//J			m_strCustomerID.Format(TEXT("0x%04x"), custId);
			opts = lic.GetOptions();

			m_ProdId = lic.GetProductId(licAppType);
			m_Version = lic.GetVersion(licAppType);
		}
		m_TimeStamp = 0x47a8beb0;  // lic.GetTrialStartDate(licSubType);
		m_Time = m_TimeStamp;
		m_Date = m_TimeStamp;

		m_bOptionLite = (opts & kLicNotLite) ? TRUE : FALSE;
		m_bOptionNoTimeout = (opts & kLicNoTimeout) ? TRUE : FALSE;
		m_bOptionNoExpire = (opts & kLicNoExpiration) ? TRUE : FALSE;
		m_bIsRegistered = (opts & kLicRegisterd) ? TRUE : FALSE;
		m_bProEdition = opts & kLicProEdition ? TRUE : FALSE;
		m_bIPOD = opts & kLicIPOD ? TRUE : FALSE;
	}

	CUTILprod* pProd = CDCMLicense2::GetUtilProd(licAppType, licSubType);
	if(pProd)
	{
		m_Uid.Format(_T("%08x"), pProd->GetUID());
		delete pProd;
	}
	else
		m_Uid = _T("");

	UpdateData(FALSE);
}


void DCMLicenseGenDlg::UpdateOptionsFlags()
{
	UpdateData();

	m_strOptionsMask.Format(TEXT("0x%016I64X"),GetOptBitsNew());

	char buf[256];
	CDCMLicense::GetKeyString(buf, 256, TRUE);
	CUTILprodinfo2 ip;
	unsigned long uidp;
	CUTILprod* pProd = CDCMLicense2::GetUtilProd(m_CurAppType, m_licSubType);

	if(pProd)
	{
		pProd->TextToInfo(&ip, &uidp, buf);
		
		m_KeyVal = buf;
		delete pProd;
	}

	UpdateData(FALSE);

}


unsigned __int64 DCMLicenseGenDlg::GetOptBitsNew()
{
	unsigned __int64 opts = 0;

	UpdateData();

	opts |= m_bOptionLite == TRUE ? kLicNotLite : 0;
	opts |= m_bOptionNoTimeout == TRUE ? kLicNoTimeout : 0;
	opts |= m_bOptionNoExpire == TRUE ? kLicNoExpiration : 0;
	opts |= m_bIsRegistered == TRUE ? kLicRegisterd : 0;
	opts |= m_bProEdition == TRUE ? kLicProEdition : 0;
	opts |= m_bIPOD == TRUE ? kLicIPOD : 0;

	return opts;
}


void DCMLicenseGenDlg::OnEnChangeEditKey()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
	m_BtnDecodeCustom.EnableWindow(TRUE);

}

void DCMLicenseGenDlg::OnEnChangeEditVersion()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
}

void DCMLicenseGenDlg::OnBnClickedButtonReload()
{
	// TODO: Add your control notification handler code here

	UpdateOptions();

}

void DCMLicenseGenDlg::OnBnClickedSetoptionsButton()
{
	// TODO: Add your control notification handler code here
	CUTILprod *pProd = NULL;
	CUTILprodinfo2 prodinfo;
	int prodId =  CDCMLicense::GetCurProdId(m_CurAppType);  //   kProdIDNone;  // 54; // kProdIdWintube;

	UpdateData();

	pProd = CDCMLicense2::GetUtilProd(m_CurAppType, m_licSubType);

	if(!pProd)
		return;

	TCHAR * ptrRet=0;
//	DWORD uid = strtoul(m_Uid, &ptrRet, 16);
	DWORD uid = _tcstoul(m_Uid, &ptrRet, 16);

	pProd->SetTrialStartDate(m_TimeStamp);
	pProd->GetInfo(&prodinfo);
	prodinfo.option = GetOptBitsNew();

	unsigned short custId = 0; // (unsigned short)strtoul(m_strCustomerID, &ptrRet, 16);;
	prodinfo.customer	= custId != 0 ? custId : prodinfo.customer;
	prodinfo.product	= m_ProdId;
	prodinfo.version	= m_Version;

	pProd->SetInfo(&prodinfo, uid);

//	m_ctrlSetOptButton.EnableWindow(FALSE);
	m_BtnDecodeCustom.EnableWindow(FALSE);

	delete pProd;

}

void DCMLicenseGenDlg::OnBnClickedButtonCopyKey2Clipboard()
{
	// TODO: Add your control notification handler code here
}

void DCMLicenseGenDlg::OnBnClickedCheckProEdition()
{
	// TODO: Add your control notification handler code here
	UpdateOptionsFlags();
}

void DCMLicenseGenDlg::OnBnClickedOptLiteCheck()
{
	// TODO: Add your control notification handler code here
	UpdateOptionsFlags();
}

void DCMLicenseGenDlg::OnBnClickedNotimeoutOptCheck()
{
	// TODO: Add your control notification handler code here
	UpdateOptionsFlags();
}

void DCMLicenseGenDlg::OnBnClickedNoexpireOptCheck()
{
	// TODO: Add your control notification handler code here
	UpdateOptionsFlags();
}

void DCMLicenseGenDlg::OnBnClickedCheckRegistered()
{
	// TODO: Add your control notification handler code here
	UpdateOptionsFlags();
}

void DCMLicenseGenDlg::OnBnClickedCheckIPOD()
{
	// TODO: Add your control notification handler code here
	UpdateOptionsFlags();
}

void DCMLicenseGenDlg::OnBnClickedButtonDecodeCustomKey()
{
	// TODO: Add your control notification handler code here

	UpdateOptions(true);

}

void DCMLicenseGenDlg::OnBnClickedCheckEmbeddedUid()
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if ( m_bEmbededUID ) 
	{
		GetDlgItem(IDC_EDIT_KEY)->EnableWindow(TRUE);
		m_BtnDecodeCustom.EnableWindow(TRUE);
		GetDlgItem(IDC_CHECK_PRO_EDITION)->EnableWindow(FALSE);
		GetDlgItem(IDC_CHECK_REGISTERED)->EnableWindow(FALSE);
		GetDlgItem(IDC_NOEXPIRE_OPT_CHECK)->EnableWindow(FALSE);
		GetDlgItem(IDC_NOTIMEOUT_OPT_CHECK)->EnableWindow(FALSE);
		GetDlgItem(IDC_OPT_LITE_CHECK)->EnableWindow(FALSE);
		GetDlgItem(IDC_CHECK_IPOD)->EnableWindow(FALSE);
	}
	else
	{
		GetDlgItem(IDC_EDIT_KEY)->EnableWindow(FALSE);
		m_BtnDecodeCustom.EnableWindow(FALSE);
		GetDlgItem(IDC_CHECK_PRO_EDITION)->EnableWindow(TRUE);
		GetDlgItem(IDC_CHECK_REGISTERED)->EnableWindow(TRUE);
		GetDlgItem(IDC_NOEXPIRE_OPT_CHECK)->EnableWindow(TRUE);
		GetDlgItem(IDC_NOTIMEOUT_OPT_CHECK)->EnableWindow(TRUE);
		GetDlgItem(IDC_OPT_LITE_CHECK)->EnableWindow(TRUE);
		GetDlgItem(IDC_CHECK_IPOD)->EnableWindow(TRUE);
	}

//	UpdateData(FALSE);

}
